﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TDT.Land.Entity;
using TDT.Land.Utilities;
using TDT.Land.BusinessLogic;
using TDT.Land.Models;
using System.Web.Routing;
using System.IO;

namespace TDT.Land.Web.Controllers
{
    public class CusLandController : BaseController
    {
        //
        // GET: /Admin/
        //[Extent.CustomAuthorize(Roles = LandType.E_ADMIN + "|"+ LandType.E_ADMIN_ROOT)]
        [CustomAuthorize(Roles = "2")]
        public ActionResult Index()
        {
            return View();
        }
        [CustomAuthorize(Roles = "2")]
        public ActionResult AD_openEdit(string id)
        {
            Cus_Land LandInfo = new Cus_Land();

            Guid ID = !string.IsNullOrEmpty(id) ? Guid.Parse(id) : Guid.Empty;            

            LandInfo = AdminRepo.Instance.GetCusLandById(ID);
            //tại đây lấy thêm các bộ enum kèm theo để truyền vào dropdownlist

            List<Cat_LandModel> lstHomeType = AdminRepo.Instance.GetCatLands(CategoryType.E_LAND_HOME_TYPE.ToString());
            List<Cat_ProvinceModel> lstProvince = AdminRepo.Instance.GetProvinces();
            List<Cat_WardModel> lstWard = AdminRepo.Instance.GetWards();
            List<Cat_DistrictModel> lstDistrict = AdminRepo.Instance.GetDistricts();
            List<Cat_LandModel> lstCatLand = AdminRepo.Instance.GetCatLands(CategoryType.E_LAND_BDS_TYPE.ToString());
            List<Cat_BDSTypeModel> lstCatBDSType = AdminRepo.Instance.GetBDSTypes();

            string HomeTypeID = string.Empty;
            string ProvinceID = string.Empty;
            string WardID = string.Empty;
            string DistricID = string.Empty;
            string CatLandID = string.Empty;
            string BDSTypeID = string.Empty;
            if (LandInfo != null)
            {
                if (LandInfo.LandID != null)
                {
                    HomeTypeID = LandInfo.LandID.ToString();
                }

                if (LandInfo.ProvinceID != null)
                {
                    ProvinceID = LandInfo.ProvinceID.ToString();
                }
                if (LandInfo.DictrictID != null)
                {
                    DistricID = LandInfo.DictrictID.ToString();
                }
                if (LandInfo.WardID != null)
                {
                    WardID = LandInfo.WardID.ToString();
                }
                if (LandInfo.LandID != null)
                {
                    CatLandID = LandInfo.LandID.ToString();
                }
                if (LandInfo.BDSTypeID != null)
                {
                    BDSTypeID = LandInfo.BDSTypeID.ToString();
                }

            }
            ViewBag.select_Ward = Commons.ConvertToSelectListItems(lstWard, Cat_WardModel.FieldName.Id, Cat_WardModel.FieldName.WardName, WardID);
            ViewBag.select_District = Commons.ConvertToSelectListItems(lstDistrict, Cat_DistrictModel.FieldName.Id, Cat_DistrictModel.FieldName.DistrictName, DistricID);
            ViewBag.select_Province = Commons.ConvertToSelectListItems(lstProvince, Cat_ProvinceModel.FieldName.Id, Cat_ProvinceModel.FieldName.ProvinceName, ProvinceID);
            ViewBag.select_Direction = Commons.ConvertToSelectListItems(typeof(DirectionType));
            ViewBag.select_LegalPaper = Commons.ConvertToSelectListItems(typeof(LegalPaperType));
            ViewBag.select_Currency = Commons.ConvertToSelectListItems(typeof(CurrencyType));
            ViewBag.select_PuposeType = Commons.ConvertToSelectListItems(typeof(PuposeType));

            ViewBag.select_CatLand = Commons.ConvertToSelectListItems(lstCatLand, Cat_LandModel.FieldName.Id, Cat_LandModel.FieldName.LandCategoryName, CatLandID);
            ViewBag.select_CatBDSType = Commons.ConvertToSelectListItems(lstCatBDSType, Cat_BDSTypeModel.FieldName.Id, Cat_BDSTypeModel.FieldName.BDSTypeName, BDSTypeID);

            //<td>@Html.DropDownListFor(m => m.LandID, (List<SelectListItem>)ViewBag.select_CatLand, new { style = "width:138px" })




            ViewBag.select_LandStatus = Commons.ConvertToSelectListItems(typeof(LandStatus));

            return View("CusLand_Edit", LandInfo);

            //return PartialView("CusLand_Edit", LandInfo);
        }
        [CustomAuthorize(Roles = "2")]
        public ActionResult AD_CusLandList()
        {
            List<Cus_LandModel> lstLand = AdminRepo.Instance.GetCusLands();
            ViewBag.lstLand = lstLand;
            return View();
        }
        [CustomAuthorize(Roles = "2")]
        public ActionResult AD_GetCusLand(int NumPage, int NumRow)
        {
            List<Cus_LandModel> lstLand = AdminRepo.Instance.GetCusLands(NumPage, NumRow);
            return PartialView("UC_AD_Cus_Land_List", lstLand);
        }


        [HttpPost]
        [ValidateInput(false)]
        [CustomAuthorize(Roles = "2")]
        public ActionResult LandAction(Cus_Land model, HttpPostedFileBase NAMEbtnUpload, FormCollection forms)
        {
            bool result = false;
            if (model.ID == null || model.ID == Guid.Empty) // Tạo mới
            {
                model.ID = Guid.NewGuid();
                result = AdminRepo.Instance.CreateCusLand(model, UserName);
            }
            else //Chinh sua
            {
                result = AdminRepo.Instance.UpdateCusLand(model, UserName);
            }
            return Content(result.ToString());
        }
       
        [CustomAuthorize(Roles = "2")]
        public ActionResult AD_LandDelete(string Ids)
        {
            List<string> lstIDStrs = Ids.Split(';').ToList();
            List<Guid> lstID = new List<Guid>();
            foreach (var item in lstIDStrs)
            {
                Guid ID = Guid.Empty;
                Guid.TryParse(item, out ID);
                if (ID != Guid.Empty)
                    lstID.Add(ID);
            }
            bool result = false;
            result = AdminRepo.Instance.DeleteCusLand(lstID, UserName);

            string _statusDelete = "00";
            if (result)
                _statusDelete = "01";

            return Content(_statusDelete);
        }

        #region user Site


        public ActionResult GetCusLand()
        {
            List<Cus_LandModel> lstLand = AdminRepo.Instance.GetCusLands();
            ViewBag.lstCusLand = lstLand;
            return View();
        }
        public ActionResult ViewCusLand(string id)
        {
            Guid ID = id != string.Empty ? Guid.Parse(id) : Guid.Empty;
            Cus_LandModel CusLand = AdminRepo.Instance.GetCusLandViewById(ID);
            ViewBag.PrLand = CusLand;
            return View();
        }

        public ActionResult SearchLand()
        {
            Guid Province = Guid.Empty;
            Guid District = Guid.Empty;
            Guid Ward = Guid.Empty;
            Guid HomeType = Guid.Empty;
            Guid BDSType = Guid.Empty;
            Guid.TryParse(Request.Params["select_Province"].ToString(), out Province);
            Guid.TryParse(Request.Params["DistrictId"].ToString(), out District);
            Guid.TryParse(Request.Params["WardId"].ToString(), out Ward);
            Guid.TryParse(Request.Params["select_HomeType"].ToString(), out HomeType);
            Guid.TryParse(Request.Params["select_BDSType"].ToString(), out BDSType);

            List<Cus_LandModel> lstLand = AdminRepo.Instance.GetCusLandsBySearch(1, 10, Province, District, Ward, HomeType, BDSType);
            return PartialView("UC_Cus_Land_List", lstLand);
        }

        /// <summary>
        /// View user site
        /// </summary>
        /// <param name="NumPage"></param>
        /// <param name="NumRow"></param>
        /// <returns></returns>
        public ActionResult LandList()
        {
            List<Cus_LandModel> lstLand = AdminRepo.Instance.GetCusLands(1, 10);
            ViewBag.lstLand = lstLand;
            return PartialView("UC_Cus_Land_List", new Cus_LandModel());

        }

        [CustomAuthorize(Roles = "1")]
        public ActionResult NewCusLand(string id)
        {

            Guid ID = id != string.Empty ? Guid.Parse(id) : Guid.Empty;
            Cus_Land LandInfo = AdminRepo.Instance.GetCusLandById(ID);
            //tại đây lấy thêm các bộ enum kèm theo để truyền vào dropdownlist

            List<Cat_LandModel> lstHomeType = AdminRepo.Instance.GetCatLands(CategoryType.E_LAND_HOME_TYPE.ToString());
            List<Cat_ProvinceModel> lstProvince = AdminRepo.Instance.GetProvinces();
            List<Cat_WardModel> lstWard = AdminRepo.Instance.GetWards();
            List<Cat_DistrictModel> lstDistrict = AdminRepo.Instance.GetDistricts();
            List<Cat_LandModel> lstCatLand = AdminRepo.Instance.GetCatLands(CategoryType.E_LAND_BDS_TYPE.ToString());
            List<Cat_BDSTypeModel> lstCatBDSType = AdminRepo.Instance.GetBDSTypes();

            string HomeTypeID = string.Empty;
            string ProvinceID = string.Empty;
            string WardID = string.Empty;
            string DistricID = string.Empty;
            string CatLandID = string.Empty;
            string BDSTypeID = string.Empty;
            if (LandInfo != null)
            {
                if (LandInfo.LandID != null)
                {
                    HomeTypeID = LandInfo.LandID.ToString();
                }

                if (LandInfo.ProvinceID != null)
                {
                    ProvinceID = LandInfo.ProvinceID.ToString();
                }
                if (LandInfo.DictrictID != null)
                {
                    DistricID = LandInfo.DictrictID.ToString();
                }
                if (LandInfo.WardID != null)
                {
                    WardID = LandInfo.WardID.ToString();
                }
                if (LandInfo.LandID != null)
                {
                    CatLandID = LandInfo.LandID.ToString();
                }
                if (LandInfo.BDSTypeID != null)
                {
                    BDSTypeID = LandInfo.BDSTypeID.ToString();
                }

            }
            ViewBag.select_Ward = Commons.ConvertToSelectListItems(lstWard, Cat_WardModel.FieldName.Id, Cat_WardModel.FieldName.WardName, WardID);
            ViewBag.select_District = Commons.ConvertToSelectListItems(lstDistrict, Cat_DistrictModel.FieldName.Id, Cat_DistrictModel.FieldName.DistrictName, DistricID);
            ViewBag.select_Province = Commons.ConvertToSelectListItems(lstProvince, Cat_ProvinceModel.FieldName.Id, Cat_ProvinceModel.FieldName.ProvinceName, ProvinceID);
            ViewBag.select_Direction = Commons.ConvertToSelectListItems(typeof(DirectionType));
            ViewBag.select_LegalPaper = Commons.ConvertToSelectListItems(typeof(LegalPaperType));
            ViewBag.select_Currency = Commons.ConvertToSelectListItems(typeof(CurrencyType));
            ViewBag.select_PuposeType = Commons.ConvertToSelectListItems(typeof(PuposeType));

            ViewBag.select_CatLand = Commons.ConvertToSelectListItems(lstCatLand, Cat_LandModel.FieldName.Id, Cat_LandModel.FieldName.LandCategoryName, CatLandID);
            ViewBag.select_CatBDSType = Commons.ConvertToSelectListItems(lstCatBDSType, Cat_BDSTypeModel.FieldName.Id, Cat_BDSTypeModel.FieldName.BDSTypeName, BDSTypeID);

            //<td>@Html.DropDownListFor(m => m.LandID, (List<SelectListItem>)ViewBag.select_CatLand, new { style = "width:138px" })




            ViewBag.select_LandStatus = Commons.ConvertToSelectListItems(typeof(LandStatus));

            return PartialView("US_UC_UserNewLand", LandInfo);
        }

        #endregion


    }
}
